﻿using System;
using System.Linq;
using System.Reflection;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using TL_Cloud.Core.Entities;

namespace TL_Cloud.Models.BookShop
{
    public partial class AppBookShopContext : DbContext
    {
        public AppBookShopContext()
        {
        }

        public AppBookShopContext(DbContextOptions<AppBookShopContext> options)
            : base(options)
        {
        }

        public virtual DbSet<Base_ClassInfo> BaseClassInfo { get; set; }
        public virtual DbSet<Base_GradeInfo> BaseGradeInfo { get; set; }
        public virtual DbSet<Base_StudentInfo> BaseStudentInfo { get; set; }
        public virtual DbSet<Base_SubjectInfo> BaseSubjectInfo { get; set; }
        public virtual DbSet<Base_TeachClass> BaseTeachClass { get; set; }
        public virtual DbSet<Base_TeacherInfo> BaseTeacherInfo { get; set; }
        public virtual DbSet<Base_User> BaseUser { get; set; }
        public virtual DbSet<Bk_BuyInfo> BkBuyInfo { get; set; }
        public virtual DbSet<Bk_Collect> BkCollect { get; set; }
        public virtual DbSet<Bk_Comment> BkComment { get; set; }
        public virtual DbSet<Bk_DownloadRecord> BkDownloadRecord { get; set; }
        public virtual DbSet<Bk_Info> BkInfo { get; set; }
        public virtual DbSet<Bk_OtherResourceUrl> BkOtherResourceUrl { get; set; }
        public virtual DbSet<Bk_Pics> BkPics { get; set; }
        public virtual DbSet<Bk_Recommend> BkRecommend { get; set; }
        public virtual DbSet<Bk_RecommendDetail> BkRecommendDetail { get; set; }
        public virtual DbSet<Bk_StoreUrl> BkStoreUrl { get; set; }
        public virtual DbSet<Bk_TypeInfo> BkTypeInfo { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
                 optionsBuilder.UseSqlServer("Server=.;Initial Catalog=TL_BookShop;User ID=sa;Password=sa123");
            }
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            string dirPath = AppDomain.CurrentDomain.BaseDirectory;
            Assembly assembly = Assembly.LoadFrom(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TL_Cloud.Models.BookShop.dll"));
            Type[] types = assembly.GetTypes();
            foreach (Type type in types.Where(n => !n.IsAbstract && n.BaseType.Name.ToLower() == "baseentity"))
            {
                BaseEntity be = (BaseEntity)type.GetConstructor(new Type[] { }).Invoke(new Object[] { });
                be.Mapping(modelBuilder);
            }
            base.OnModelCreating(modelBuilder);
        }
    }
}
